package james.gan.common;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class ExtensionFactory<T> {

	protected static final Map<Class<?>, Object> EXTENSIONS = new ConcurrentHashMap<>();

	public static <T> void registe(Class<?> clz, T t) {
		EXTENSIONS.put(clz, t);
	}

	@SuppressWarnings("unchecked")
	public static <T> T get(Class<?> clz) {
		return (T) EXTENSIONS.get(clz);
	}

	public abstract Class<?> getExtensionClass();

	public ExtensionFactory() {
		init();
	}

	public void init() {
		ExtensionLoader.load(getExtensionClass(), ExtensionLoader.class.getClassLoader()).stream().forEach(e -> {
			EXTENSIONS.put(e.getClass(), e);
		});
	}
}
